package com.ccyl.servlet;

import com.ccyl.entity.Admin_user;
import com.ccyl.service.LoginService;
import com.ccyl.service.impl.LoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/adminLogin")
public class UserServlet extends HttpServlet {

    private LoginService loginService=new LoginServiceImpl();
    /**
     * 处理登录的业务逻辑
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doPost(req, resp);
        String username=req.getParameter("user");
        String userpassword=req.getParameter("password");
        Admin_user admin_user=loginService.login(username,userpassword);
        if (admin_user == null) {
            req.setAttribute("error", "用户名或密码错误!!!");
            req.getRequestDispatcher("adminLogin.jsp").forward(req, resp);
        } else {
            req.setAttribute("user",username);
            req.getRequestDispatcher("admin.jsp").forward(req, resp);
        }
    }
}
